<template>
  <view class="container">
    <view class="prototype-container">
      <view class="prototype">
        <!-- 状态栏 -->
        <view class="status-bar">
          <view class="time">{{currentTime}}</view>
          <view class="status-icons">
            <text>●●●</text>
            <text>📶</text>
            <text>🔋</text>
          </view>
        </view>
        
        <!-- 头部 -->
        <view class="header">
          <view class="logo-section">
            <view class="logo">
              <text style="color: #2989d8; font-size: 20px;">医</text>
            </view>
            <view class="app-name">智慧医疗</view>
          </view>
          <view class="header-right">
            <text class="header-icon" @click="navigateToSettings">⚙️</text>
            <text class="header-icon">⊙</text>
          </view>
        </view>
        
        <!-- 活动信息 -->
        <view class="event-info">
          <view style="display: flex; align-items: flex-start;">
            <view class="year-badge">
              2024
            </view>
            <view>
              <view class="event-title">医疗会晤峰会</view>
              <view class="event-subtitle">医疗创新与合作</view>
            </view>
          </view>
          <view class="event-location">中国 · 广州 | 2025.06.15-17</view>
        </view>
        
        <!-- 轮播广告 -->
        <view class="banner-wrapper">
          <ad-banner></ad-banner>
        </view>
        
        <!-- 菜单网格 -->
        <view class="menu-grid">
          <view class="menu-item" @click="openModal('活动日程')">
            <view class="menu-icon">📅</view>
            <view class="menu-text">活动日程</view>
          </view>
          <view class="menu-item" @click="openModal('注意事项')">
            <view class="menu-icon">📋</view>
            <view class="menu-text">注意事项</view>
          </view>
          <view class="menu-item" @click="openModal('酒店住宿')">
            <view class="menu-icon">🏨</view>
            <view class="menu-text">酒店住宿</view>
          </view>
          <view class="menu-item" @click="openModal('交通服务')">
            <view class="menu-icon">🚗</view>
            <view class="menu-text">交通服务</view>
          </view>
          <view class="menu-item" @click="openModal('出行指引')">
            <view class="menu-icon">🧭</view>
            <view class="menu-text">出行指引</view>
          </view>
          <view class="menu-item" @click="openModal('用餐安排')">
            <view class="menu-icon">🍽️</view>
            <view class="menu-text">用餐安排</view>
          </view>
          <view class="menu-item" @click="openModal('签到')">
            <view class="menu-icon">✍️</view>
            <view class="menu-text">签到</view>
          </view>
          <view class="menu-item" @click="openModal('投票')">
            <view class="menu-icon">🗳️</view>
            <view class="menu-text">投票</view>
          </view>
          <view class="menu-item" @click="navigateToHomePage">
            <view class="menu-icon">🏠</view>
            <view class="menu-text">首页</view>
          </view>
        </view>
        
        <!-- 底部 -->
        <view class="footer">
          <view class="footer-text">【主办单位】</view>
          <view class="footer-text">国家医疗健康委员会</view>
          <view class="footer-text">【承办单位】</view>
          <view class="footer-text">广州医科大学附属第一医院</view>
        </view>
        
        <!-- 弹窗 -->
        <view class="modal" :class="{'show': showModal}" @click="closeModal">
          <view class="modal-content" @click.stop>
            <view class="modal-header">
              <view class="modal-title">{{modalTitle}}</view>
              <text class="close-button" @click="closeModal">×</text>
            </view>
            <view class="modal-body">
              <view v-if="isLoading" class="loading-container">
                <view class="loading-spinner"></view>
                <text class="loading-text">数据加载中...</text>
              </view>
              <rich-text v-else :nodes="modalContent"></rich-text>
            </view>
            <view class="modal-footer">
              <button class="modal-button" @click="closeModal">确定</button>
            </view>
          </view>
        </view>
      </view>
    </view>
  </view>
</template>

<script>
import AdBanner from '@/components/AdBanner.vue'
import { 
  getEventInfo, 
  getEventSchedule, 
  getEventNotices, 
  getEventHotel, 
  getEventTransport,
  getEventGuide,
  getEventDining
} from '@/api/event.js'

export default {
  components: {
    AdBanner
  },
  data() {
    return {
      currentTime: '9:41',
      showModal: false,
      modalTitle: '',
      modalContent: '',
      isLoading: false,
      eventInfo: null,
      modalData: {
        '活动日程': '加载中...',
        '注意事项': '加载中...',
        '酒店住宿': '加载中...',
        '交通服务': '加载中...',
        '出行指引': '加载中...',
        '用餐安排': '加载中...',
        '签到': '功能开发中...',
        '投票': '功能开发中...'
      },
      gradientColors: {
        primary: '#1e5799',
        secondary: '#2989d8',
        tertiary: '#207cca'
      }
    }
  },
  mounted() {
    this.updateTime()
    setInterval(this.updateTime, 1000)
    this.loadEventInfo()
    this.loadBackgroundColor()
    
    // 监听背景颜色更新事件
    uni.$on('updateBackgroundColor', this.handleBackgroundColorUpdate)
    
    // 监听打开酒店住宿模态框事件
    uni.$on('openHotelModal', () => {
      this.openModal('酒店住宿')
    })
  },
  beforeDestroy() {
    // 移除事件监听
    uni.$off('updateBackgroundColor', this.handleBackgroundColorUpdate)
    uni.$off('openHotelModal')
  },
  methods: {
    updateTime() {
      const now = new Date()
      const hours = now.getHours().toString().padStart(2, '0')
      const minutes = now.getMinutes().toString().padStart(2, '0')
      this.currentTime = `${hours}:${minutes}`
    },
    navigateToSettings() {
      uni.navigateTo({
        url: '/pages/settings/settings'
      })
    },
    navigateToHomePage() {
      uni.reLaunch({
        url: '/pages/index_home/index_home'
      })
    },
    openModal(title) {
      this.modalTitle = title
      this.showModal = true
      
      // 根据模态窗标题加载对应的数据
      switch(title) {
        case '活动日程':
          this.loadScheduleData()
          break
        case '注意事项':
          this.loadNoticesData()
          break
        case '酒店住宿':
          this.loadHotelData()
          break
        case '交通服务':
          this.loadTransportData()
          break
        case '出行指引':
          this.loadGuideData()
          break
        case '用餐安排':
          this.loadDiningData()
          break
      }
      
      // 设置模态窗内容
      this.modalContent = this.modalData[title]
    },
    closeModal() {
      this.showModal = false
    },
    // 加载会议基本信息
    async loadEventInfo() {
      try {
        this.eventInfo = await getEventInfo()
        // 更新页面显示的会议信息
        if (this.eventInfo) {
          // 可以在这里更新会议标题、时间、地点等信息
        }
      } catch (error) {
        console.error('获取会议信息失败:', error)
      }
    },
    // 加载活动日程数据
    async loadScheduleData() {
      if (this.modalData['活动日程'] !== '加载中...') return
      
      this.isLoading = true
      try {
        const scheduleData = await getEventSchedule()
        let formattedContent = ''
        
        if (scheduleData && scheduleData.length > 0) {
          scheduleData.forEach(day => {
            formattedContent += `<h4 style="margin-bottom: 10px; color: #1e5799;">${day.date}</h4><div style="margin-bottom: 15px;">`
            
            if (day.sessions && day.sessions.length > 0) {
              day.sessions.forEach(session => {
                formattedContent += `
                  <div style="margin-bottom: 8px;"><strong>${session.startTime} - ${session.endTime}</strong> ${session.title}</div>
                  <div style="margin-left: 20px; color: #666; margin-bottom: 12px;">${session.description || ''}</div>
                  <div style="margin-left: 20px; color: #333; margin-bottom: 12px;">📍 ${session.location}</div>
                `
              })
            }
            
            formattedContent += '</div>'
          })
        } else {
          formattedContent = '暂无活动日程数据'
        }
        
        this.modalData['活动日程'] = formattedContent
        this.modalContent = formattedContent
      } catch (error) {
        console.error('获取活动日程失败:', error)
        this.modalData['活动日程'] = '获取数据失败，请稍后重试'
        this.modalContent = '获取数据失败，请稍后重试'
      } finally {
        this.isLoading = false
      }
    },
    // 加载注意事项数据
    async loadNoticesData() {
      if (this.modalData['注意事项'] !== '加载中...') return
      
      this.isLoading = true
      try {
        const noticesData = await getEventNotices()
        let formattedContent = '<ul style="padding-left: 20px; line-height: 1.6;">'
        
        if (noticesData && noticesData.length > 0) {
          noticesData.forEach(notice => {
            formattedContent += `
              <li style="margin-bottom: 15px;"><strong>${notice.title}</strong><br>${notice.content}</li>
            `
          })
        } else {
          formattedContent = '暂无注意事项数据'
        }
        
        formattedContent += '</ul>'
        this.modalData['注意事项'] = formattedContent
        this.modalContent = formattedContent
      } catch (error) {
        console.error('获取注意事项失败:', error)
        this.modalData['注意事项'] = '获取数据失败，请稍后重试'
        this.modalContent = '获取数据失败，请稍后重试'
      } finally {
        this.isLoading = false
      }
    },
    // 加载酒店住宿数据
    async loadHotelData() {
      if (this.modalData['酒店住宿'] !== '加载中...') return
      
      this.isLoading = true
      try {
        const hotelData = await getEventHotel()
        let formattedContent = ''
        
        if (hotelData && hotelData.length > 0) {
          hotelData.forEach(hotel => {
            formattedContent += `
              <h4 style="margin-bottom: 10px; color: #1e5799;">${hotel.name}</h4>
              <div style="margin-bottom: 20px;">
                <div style="margin-bottom: 5px;">📍 地址：${hotel.address}</div>
                <div style="margin-bottom: 5px;">📞 电话：${hotel.phone}</div>
                <div style="margin-bottom: 5px;">💰 价格：${hotel.price}</div>
                <div style="margin-bottom: 5px;">${hotel.description}</div>
              </div>
            `
          })
        } else {
          formattedContent = '暂无酒店住宿数据'
        }
        
        this.modalData['酒店住宿'] = formattedContent
        this.modalContent = formattedContent
      } catch (error) {
        console.error('获取酒店住宿失败:', error)
        this.modalData['酒店住宿'] = '获取数据失败，请稍后重试'
        this.modalContent = '获取数据失败，请稍后重试'
      } finally {
        this.isLoading = false
      }
    },
    // 加载交通服务数据
    async loadTransportData() {
      if (this.modalData['交通服务'] !== '加载中...') return
      
      this.isLoading = true
      try {
        const transportData = await getEventTransport()
        let formattedContent = ''
        
        if (transportData && transportData.length > 0) {
          transportData.forEach(transport => {
            formattedContent += `
              <h4 style="margin-bottom: 10px; color: #1e5799;">${transport.type}</h4>
              <div style="margin-bottom: 20px;">
                <div style="margin-bottom: 5px;"><strong>${transport.route}</strong></div>
                <div style="margin-bottom: 5px;">🕒 时间：${transport.schedule}</div>
                <div style="margin-bottom: 5px;">💰 费用：${transport.price}</div>
                <div style="margin-bottom: 5px;">${transport.description}</div>
              </div>
            `
          })
        } else {
          formattedContent = '暂无交通服务数据'
        }
        
        this.modalData['交通服务'] = formattedContent
        this.modalContent = formattedContent
      } catch (error) {
        console.error('获取交通服务失败:', error)
        this.modalData['交通服务'] = '获取数据失败，请稍后重试'
        this.modalContent = '获取数据失败，请稍后重试'
      } finally {
        this.isLoading = false
      }
    },
    // 加载出行指引数据
    async loadGuideData() {
      if (this.modalData['出行指引'] !== '加载中...') return
      
      this.isLoading = true
      try {
        const guideData = await getEventGuide()
        let formattedContent = ''
        
        if (guideData && guideData.length > 0) {
          guideData.forEach(guide => {
            formattedContent += `
              <h4 style="margin-bottom: 10px; color: #1e5799;">${guide.title}</h4>
              <div style="margin-bottom: 20px;">
                <div style="margin-bottom: 10px;">${guide.content}</div>
              </div>
            `
          })
        } else {
          formattedContent = '暂无出行指引数据'
        }
        
        this.modalData['出行指引'] = formattedContent
        this.modalContent = formattedContent
      } catch (error) {
        console.error('获取出行指引失败:', error)
        this.modalData['出行指引'] = '获取数据失败，请稍后重试'
        this.modalContent = '获取数据失败，请稍后重试'
      } finally {
        this.isLoading = false
      }
    },
    // 加载用餐安排数据
    async loadDiningData() {
      if (this.modalData['用餐安排'] !== '加载中...') return
      
      this.isLoading = true
      try {
        const diningData = await getEventDining()
        let formattedContent = ''
        
        if (diningData && diningData.length > 0) {
          diningData.forEach(dining => {
            formattedContent += `<h4 style="margin-bottom: 10px; color: #1e5799;">${dining.date}</h4><div style="margin-bottom: 20px;">`
            
            if (dining.meals && dining.meals.length > 0) {
              dining.meals.forEach(meal => {
                formattedContent += `
                  <div style="margin-bottom: 15px;">
                    <div style="margin-bottom: 5px;"><strong>🍲 ${meal.mealType}</strong> (${meal.time})</div>
                    <div style="margin-bottom: 5px;">📍 地点：${meal.location}</div>
                    <div style="margin-bottom: 5px;">🍴 菜单：${meal.menu}</div>
                    <div style="margin-bottom: 5px; color: #666;">📝 备注：${meal.description}</div>
                  </div>
                `
              })
            }
            
            formattedContent += '</div>'
          })
        } else {
          formattedContent = '暂无用餐安排数据'
        }
        
        this.modalData['用餐安排'] = formattedContent
        this.modalContent = formattedContent
      } catch (error) {
        console.error('获取用餐安排失败:', error)
        this.modalData['用餐安排'] = '获取数据失败，请稍后重试'
        this.modalContent = '获取数据失败，请稍后重试'
      } finally {
        this.isLoading = false
      }
    },
    loadBackgroundColor() {
      // 从本地存储加载保存的背景颜色
      const savedColor = uni.getStorageSync('appBackgroundColor')
      if (savedColor) {
        this.updateGradientColors(savedColor)
      }
    },
    handleBackgroundColorUpdate(color) {
      this.updateGradientColors(color)
    },
    updateGradientColors(baseColor) {
      // 根据基础颜色生成渐变色
      this.gradientColors.primary = baseColor
      
      // 计算次要颜色（稍微亮一些）
      const lighterColor = this.lightenColor(baseColor, 20)
      this.gradientColors.secondary = lighterColor
      
      // 计算第三颜色（稍微暗一些）
      const darkerColor = this.darkenColor(baseColor, 10)
      this.gradientColors.tertiary = darkerColor
    },
    lightenColor(color, percent) {
      const num = parseInt(color.replace('#', ''), 16)
      const amt = Math.round(2.55 * percent)
      const R = (num >> 16) + amt
      const G = (num >> 8 & 0x00FF) + amt
      const B = (num & 0x0000FF) + amt
      return '#' + (
        0x1000000 +
        (R < 255 ? (R < 1 ? 0 : R) : 255) * 0x10000 +
        (G < 255 ? (G < 1 ? 0 : G) : 255) * 0x100 +
        (B < 255 ? (B < 1 ? 0 : B) : 255)
      ).toString(16).slice(1)
    },
    darkenColor(color, percent) {
      const num = parseInt(color.replace('#', ''), 16)
      const amt = Math.round(2.55 * percent)
      const R = (num >> 16) - amt
      const G = (num >> 8 & 0x00FF) - amt
      const B = (num & 0x0000FF) - amt
      return '#' + (
        0x1000000 +
        (R > 0 ? (R > 255 ? 255 : R) : 0) * 0x10000 +
        (G > 0 ? (G > 255 ? 255 : G) : 0) * 0x100 +
        (B > 0 ? (B > 255 ? 255 : B) : 0)
      ).toString(16).slice(1)
    }
  }
}
</script>

<style>
/* 容器 */
.container {
  min-height: 100vh;
  background-color: #f5f7fa;
  padding: 20px;
}
/* 原型 */
.prototype-container {
  width: 375px;
  height: 812px;
  margin: 0 auto;
  border-radius: 40px;
  overflow: hidden;
  position: relative;
  box-shadow: 0 10px 30px rgba(0, 0, 0, 0.2);
  border: 10px solid #000;
  background-color: #fff;
}

.prototype {
  width: 100%;
  height: 100%;
  overflow: hidden;
  position: relative;
  background: linear-gradient(to bottom, v-bind('gradientColors.primary'), v-bind('gradientColors.secondary'), v-bind('gradientColors.tertiary'));
  transition: background 0.3s ease;
}
/* 状态栏 */
.status-bar {
  height: 44px;
  width: 100%;
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 0 20px;
  color: white;
  font-size: 14px;
  font-weight: 600;
}
/* 时间 */
.time {
  font-weight: bold;
}
/* 状态图标 */
.status-icons {
  display: flex;
  gap: 5px;
}

.header {
  padding: 15px 20px;
  display: flex;
  align-items: center;
  justify-content: space-between;
}
/* 标志 */
.logo-section {
  display: flex;
  align-items: center;
}
/* 标志 */
.logo {
  width: 36px;
  height: 36px;
  background-color: white;
  border-radius: 50%;
  display: flex;
  align-items: center;
  justify-content: center;
  margin-right: 10px;
}
/* 标志 */
.app-name {
  color: white;
  font-size: 18px;
  font-weight: bold;
}
/* 标志 */
.header-right {
  display: flex;
  align-items: center;
  background-color: rgba(255, 255, 255, 0.2);
  border-radius: 20px;
  padding: 5px 10px;
}
/* 标志 */
.header-icon {
  color: white;
  margin: 0 5px;
  font-size: 18px;
}
/* 事件信息 */
.event-info {
  padding: 15px 20px;
  color: white;
  text-align: left;
}
/* 年份徽章 */
.year-badge {
  font-size: 32px;
  font-weight: bold;
  margin-bottom: 10px;
  display: flex;
  align-items: center;
}
/* 年份徽章 */
.year-badge::after {
  content: "";
  width: 1px;
  height: 30px;
  background-color: rgba(255, 255, 255, 0.5);
  margin: 0 15px;
}
/* 事件标题 */
.event-title {
  font-size: 20px;
  font-weight: bold;
  margin-bottom: 5px;
}
/* 事件副标题 */
.event-subtitle {
  font-size: 16px;
  margin-bottom: 5px;
}
/* 事件地点 */  
.event-location {
  font-size: 14px;
  opacity: 0.9;
}
/* 菜单 */
.menu-grid {
  padding: 10px 20px 20px;
  display: grid;
  grid-template-columns: 1fr 1fr;
  grid-template-rows: 1fr 1fr;
  grid-gap: 15px;
  margin-top: 10px;
  max-height: 220px;
  overflow-y: auto;
  scrollbar-width: thin;
  scrollbar-color: rgba(255, 255, 255, 0.3) transparent;
}
/* 菜单滚动条 */
.menu-grid::-webkit-scrollbar {
  width: 6px;
}
/* 菜单滚动条轨道 */
.menu-grid::-webkit-scrollbar-track {
  background: transparent;
}
/* 菜单滚动条滑块 */
.menu-grid::-webkit-scrollbar-thumb {
  background-color: rgba(255, 255, 255, 0.3);
  border-radius: 3px;
}
/* 菜单 */
.menu-item {
  background-color: rgba(255, 255, 255, 0.2);
  border-radius: 12px;
  padding: 10px;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  color: white;
  text-align: center;
  cursor: pointer;
  transition: all 0.3s;
  height: 100px;
}
/* 菜单点击 */
.menu-item:active {
  background-color: rgba(255, 255, 255, 0.3);
  transform: translateY(-3px);
}
/* 菜单图标 */
.menu-icon {
  font-size: 24px;
  margin-bottom: 10px;
}
/* 菜单文字 */
.menu-text {
  font-size: 16px;
  font-weight: bold;
}
/* 底部 */
.footer {
  position: absolute;
  bottom: 0;
  left: 0;
  right: 0;
  padding: 15px 20px;
  color: white;
  text-align: center;
  font-size: 12px;
  background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 1440 320'%3E%3Cpath fill='%23000' fill-opacity='0.2' d='M0,224L80,213.3C160,203,320,181,480,181.3C640,181,800,203,960,213.3C1120,224,1280,224,1360,208L1440,192L1440,320L1360,320C1280,320,1120,320,960,320C800,320,640,320,480,320C320,320,160,320,80,320L0,320Z'%3E%3C/path%3E%3C/svg%3E");
  background-size: cover;
  background-position: center top;
  height: 100px;
  display: flex;
  flex-direction: column;
  justify-content: flex-end;
  z-index: 5;
}
/* 底部文字 */
.footer-text {
  margin-bottom: 5px;
}
/* 模态框 */
.modal {
  display: none;
  position: absolute;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  background-color: rgba(0, 0, 0, 0.5);
  z-index: 10;
  justify-content: center;
  align-items: center;
}
/* 模态框显示 */
.modal.show {
  display: flex;
}
/* 模态框内容 */  
.modal-content {
  background-color: white;
  width: 80%;
  border-radius: 15px;
  padding: 20px;
  box-shadow: 0 5px 15px rgba(0, 0, 0, 0.3);
}
/* 模态框头部 */
.modal-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 15px;
  padding-bottom: 10px;
  border-bottom: 1px solid #eee;
}
/* 模态框标题 */
.modal-title {
  font-size: 18px;
  font-weight: bold;
  text-align: center;
  flex: 1;
}
/* 模态框关闭按钮 */
.close-button {
  font-size: 24px;
  color: #777;
  cursor: pointer;
}
/* 模态框内容 */
.modal-body {
  padding: 15px;
  max-height: 400px;
  overflow-y: auto;
}
/* 模态框底部 */
.modal-footer {
  border-top: 1px solid #eee;
  padding: 10px 15px;
  text-align: center;
}
/* 模态框按钮 */
.modal-button {
  background-color: #1e5799;
  color: white;
  border: none;
  padding: 8px 15px;
  border-radius: 5px;
  cursor: pointer;
}
/* 加载指示器 */
.loading-container {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  padding: 30px;
}

.loading-spinner {
  width: 40px;
  height: 40px;
  border: 4px solid #f3f3f3;
  border-top: 4px solid #2989d8;
  border-radius: 50%;
  animation: spin 1s linear infinite;
  margin-bottom: 15px;
}

.loading-text {
  color: #666;
  font-size: 16px;
}

@keyframes spin {
  0% { transform: rotate(0deg); }
  100% { transform: rotate(360deg); }
}

/* 轮播广告包装器 */
.banner-wrapper {
  padding: 0 20px;
  margin-bottom: 10px;
}
</style>
